/*
	The MIT License

	Copyright (c) 2010 IFMO/GameDev Studio

	Permission is hereby granted, free of charge, to any person obtaining a copy
	of this software and associated documentation files (the "Software"), to deal
	in the Software without restriction, including without limitation the rights
	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
	copies of the Software, and to permit persons to whom the Software is
	furnished to do so, subject to the following conditions:

	The above copyright notice and this permission notice shall be included in
	all copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
	THE SOFTWARE.
*/


#include "d3d_local.h"


/*-----------------------------------------------------------------------------
	Scene rendering stuff :
-----------------------------------------------------------------------------*/


//
//	ERScene::RenderView
//
void ERScene::RenderScene( void )
{
	HRCALL( d3ddev->SetTransform(D3DTS_VIEW,		&D3DXMATRIX(view_matrix.Ptr()) ) );
	HRCALL( d3ddev->SetTransform(D3DTS_PROJECTION,	&D3DXMATRIX(proj_matrix.Ptr()) ) );

	uint w, h;
	rs->GetScreenSize(w, h);
	
	IPxTexture				gbuffer[4];
	IPxTexture				gbuffer_depth;
	IPxTexture				dudv_buffer;
	IPxTexture				hdr_buffer;
	IPxTexture				hdr_buffer_copy;
		
	hdr_buffer		=	rs->rtm->AllocRenderTarget(RS_FORMAT_ARGB_U10, w, h);
	hdr_buffer_copy	=	rs->rtm->AllocRenderTarget(RS_FORMAT_ARGB_U10, w, h);
	gbuffer[0]		=	rs->rtm->AllocRenderTarget(RS_FORMAT_ARGB_U8, w, h);
	gbuffer[1]		=	rs->rtm->AllocRenderTarget(RS_FORMAT_ARGB_U8, w, h);
	gbuffer[2]		=	rs->rtm->AllocRenderTarget(RS_FORMAT_ARGB_U8, w, h);
	gbuffer[3]		=	rs->rtm->AllocRenderTarget(RS_FORMAT_ARGB_U8, w, h);
	gbuffer_depth	=	rs->rtm->AllocRenderTarget(RS_FORMAT_DEPTH_READABLE, w, h);
	dudv_buffer		=	rs->rtm->AllocRenderTarget(RS_FORMAT_ARGB_U8, w, h);

		
		//	fill up G-buffer :
		RenderDeferred( gbuffer, gbuffer_depth );

		//	render lighting and shadows :
		light_rend->SetGBuffer( gbuffer, gbuffer_depth );
		RenderLighting( hdr_buffer );
		light_rend->SetGBuffer( NULL, NULL );
		
		//	add fog :
		scr_fx->RenderFog( hdr_buffer, gbuffer_depth );	

		//	render translucent stuff :
		RenderTranslucent( hdr_buffer );

		//	render distortive stuff and do post-process :
		RenderDistortive( dudv_buffer );
		rtm->CopyTarget( hdr_buffer_copy, hdr_buffer );
		scr_fx->RenderDistortion( hdr_buffer, dudv_buffer, hdr_buffer_copy );

		//	render emissive surfaces :
		RenderEmissive( hdr_buffer );
		
		//	render particles :
		gfx->RenderGFXTranslucent( hdr_buffer, gbuffer_depth );
		
		gfx->RenderGFXDistortive( dudv_buffer, gbuffer_depth );
		rtm->CopyTarget( hdr_buffer_copy, hdr_buffer );
		scr_fx->RenderDistortion( hdr_buffer, dudv_buffer, hdr_buffer_copy );

		gfx->RemoveGFXPrimitives();
		
		
		//	restore targets :
		rs->rtm->RestoreRenderTargets();
		
		//	do HDR processing :
		scr_fx->RenderHDR( hdr_buffer );

		//	render wireframe :		
		RenderWireframe();
		
		//	render debug 
		RenderDebug();
	
	
	hdr_buffer		=	NULL;
	hdr_buffer_copy	=	NULL;
	gbuffer[0]		=	NULL;
	gbuffer[1]		=	NULL;	
	gbuffer[2]		=	NULL;	
	gbuffer[3]		=	NULL;	
	gbuffer_depth	=	NULL;	
	dudv_buffer		=	NULL;
}


//
//	ERScene::RenderSimple
//
void ERScene::RenderSimple( void )
{
}


//
//	ERScene::RenderSimple
//
void ERScene::RenderDeferred( IPxTexture *gbuffer, IPxTexture gbuffer_depth )
{
	VID_PROF_PROBE();

	rs->ResetDeviceState();
	rs->rtm->RestoreRenderTargets();

	//	set appropriate RTs :	
	rs->rtm->SetRenderTarget( 0, gbuffer[0] );
	rs->rtm->SetRenderTarget( 1, gbuffer[1] );
	rs->rtm->SetRenderTarget( 2, gbuffer[2] );
	rs->rtm->SetRenderTarget( 3, gbuffer[3] );
	rs->rtm->SetDepthStencil( gbuffer_depth );
	rs->rtm->Clear(RS_CLEAR_COLOR | RS_CLEAR_DEPTH | RS_CLEAR_STENCIL, EVec4(0,0,0,1));

	sshs->BeginShading( RS_TECH_DEFERRED );
	
		sshs->SetProjMatrix( proj_matrix );
		sshs->SetViewMatrix( view_matrix, view_position );
		sshs->SetWorldMatrix( Matrix4Identity() );

		for (uint i=0; i<entities.size(); i++) {
			ERSEntity	*ent = entities[i].To<ERSEntity>();
			ent->Render();
		}
	
	sshs->EndShading();

	rs->rtm->RestoreRenderTargets();
}


//
//	ERScene::RenderLighting
//
void ERScene::RenderLighting( IPxTexture target )
{
	VID_PROF_PROBE();
	
	light_rend->CopyDepthBuffer();

	rs->rtm->RestoreRenderTargets();
	rs->rtm->SetRenderTarget( 0, target );

	light_rend->RenderAmbient( target );

	for (uint i=0; i<lights.size(); i++) {
		ERSLight	*lt = lights[i].To<ERSLight>();
		lt->RenderLight( target );
	}
}


//
//	ERScene::RenderSimple
//
void ERScene::RenderEmissive( IPxTexture target )
{
	VID_PROF_PROBE();

	rs->rtm->SetRenderTarget( 0, target );

	sshs->BeginShading( RS_TECH_EMISSION );
	
		sshs->SetProjMatrix( proj_matrix );
		sshs->SetViewMatrix( view_matrix, view_position );
		sshs->SetWorldMatrix( Matrix4Identity() );

		for (uint i=0; i<entities.size(); i++) {
			ERSEntity	*ent = entities[i].To<ERSEntity>();
			ent->Render();
		}
	
	sshs->EndShading();
}


//
//	ERScene::RenderSimple
//
void ERScene::RenderDistortive( IPxTexture dudv_buffer )
{
	rs->rtm->SetRenderTarget( 0, dudv_buffer );
	rs->rtm->Clear(RS_CLEAR_COLOR, EVec4(0.5,0.5,0,0));

	sshs->BeginShading( RS_TECH_DISTORTION );
	
		sshs->SetProjMatrix( proj_matrix );
		sshs->SetViewMatrix( view_matrix, view_position );
		sshs->SetWorldMatrix( Matrix4Identity() );

		for (uint i=0; i<entities.size(); i++) {
			ERSEntity	*ent = entities[i].To<ERSEntity>();
			ent->Render();
		}
	
	sshs->EndShading();
}


//
//	ERScene::RenderSimple
//
void ERScene::RenderTranslucent( IPxTexture target )
{
	sshs->BeginShading( RS_TECH_TRANSLUCENT );
	
		sshs->SetProjMatrix( proj_matrix );
		sshs->SetViewMatrix( view_matrix, view_position );
		sshs->SetWorldMatrix( Matrix4Identity() );

		for (uint i=0; i<entities.size(); i++) {
			ERSEntity	*ent = entities[i].To<ERSEntity>();
			ent->Render();
		}
	
	sshs->EndShading();
}


//
//	ERScene::RenderWireframe
//
void ERScene::RenderWireframe( void )
{
	VID_PROF_PROBE();

	if (!rs_wireframe->Bool()) {
		return;
	}

	sshs->BeginShading( RS_TECH_WIREFRAME );
	
		sshs->SetProjMatrix( proj_matrix );
		sshs->SetViewMatrix( view_matrix, view_position );
		sshs->SetWorldMatrix( Matrix4Identity() );

		for (uint i=0; i<entities.size(); i++) {
			ERSEntity	*ent = entities[i].To<ERSEntity>();
			ent->Render();
		}
	
	sshs->EndShading();
}


//
//	ERScene::RenderShadowMap
//
void ERScene::RenderShadowMap( const EMatrix4 &view, const EMatrix4 &proj, IPxTexture sm_color, IPxTexture sm_depth )
{
	VID_PROF_PROBE();

	rs->ResetDeviceState();

	rs->rtm->SetRenderTarget( 0, sm_color );
	rs->rtm->SetDepthStencil( sm_depth );
	rs->rtm->Clear(RS_CLEAR_COLOR | RS_CLEAR_DEPTH, EVec4(0,0,0,1));
	//	TODO : remove clear color

	sshs->BeginShading( RS_TECH_SHADOWMAP );
	
		sshs->SetProjMatrix( proj );
		sshs->SetViewMatrix( view, EVec4(0,0,0,1) );	// view pos in unecessary
		sshs->SetWorldMatrix( Matrix4Identity() );

		for (uint i=0; i<entities.size(); i++) {
			ERSEntity	*ent = entities[i].To<ERSEntity>();
			ent->Render();
		}
	
	sshs->EndShading();
	
	rs->rtm->RestoreRenderTargets();
}


/*-----------------------------------------------------------------------------
	Debug rendering stuff :
-----------------------------------------------------------------------------*/

//
//	ERScene::InitDebugRender
//
void ERScene::InitDebugRender( void )
{
	debug_lines.reserve(1000);	
}


//
//	ERScene::RenderDebug
//
void ERScene::RenderDebug( void )
{
	sshs->SetProjMatrix( proj_matrix );
	sshs->SetViewMatrix( view_matrix, view_position );
	sshs->SetWorldMatrix( Matrix4Identity() );

	sshs->BeginShading( RS_TECH_DEBUG );
	
		if (!debug_lines.empty()) {
			rs->mss->DrawLines( debug_lines.size(), &debug_lines[0] );
		}

		debug_lines.clear();

	sshs->EndShading();
}


//
//	ERScene::DebugLine
//
void ERScene::DebugLine( const EVec3 &v0, const EVec3 &v1, const EVec4 &color )
{
	ERSLine_s	line;
	line.c0 = color;
	line.c1 = color;
	line.v0 = v0;
	line.v1 = v1;
	debug_lines.push_back(line);
}